﻿using FanKit.Transformers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Reflection;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml.Controls;

namespace TestApp4
{
    public sealed partial class MainPage : Page
    {
        //@Instance
        private readonly Lazy<ApplicationView> ViewLazy = new Lazy<ApplicationView>(() => ApplicationView.GetForCurrentView());
        private ApplicationView View => this.ViewLazy.Value;

        public MainPage()
        {
            this.InitializeComponent();

            Transformer source = new Transformer
            {
                LeftTop = new Vector2(40, 40),
                RightTop = new Vector2(300, 0),
                RightBottom = new Vector2(444, 300),
                LeftBottom = new Vector2(0, 300),
            };
            S.Points.Add(source.LeftTop.ToPoint());
            S.Points.Add(source.RightTop.ToPoint());
            S.Points.Add(source.RightBottom.ToPoint());
            S.Points.Add(source.LeftBottom.ToPoint());
            S.Points.Add(source.LeftTop.ToPoint());

            Transformer des = new Transformer
            {
                LeftTop = new Vector2(100, 100),
                RightTop = new Vector2(400, 100),
                RightBottom = new Vector2(400, 400),
                LeftBottom = new Vector2(100, 400),
            };
            D.Points.Add(des.LeftTop.ToPoint());
            D.Points.Add(des.RightTop.ToPoint());
            D.Points.Add(des.RightBottom.ToPoint());
            D.Points.Add(des.LeftBottom.ToPoint());
            D.Points.Add(des.LeftTop.ToPoint());

            var matrix = Transformer.FindHomography(source, des, out Vector2 distance);
           
            Canvas.PointerMoved += (s, e) =>
            {
                Vector2 i = e.GetCurrentPoint(Canvas).Position.ToVector2();
                Canvas.SetLeft(I, i.X);
                Canvas.SetTop(I, i.Y);


                var v1 = Vector2.Transform(i, matrix);
                var d = tfv(i, matrix);
          

                Vector2 o = Vector2.Transform(i, matrix) / (1 + Vector2.Dot(i, distance));
 

                Canvas.SetLeft(O, o.X);
                Canvas.SetTop(O, o.Y);
            };

            Init();
        }

        Vector2 tfv(Vector2 p, Matrix3x2 m)
        {
            float x = m.M11 * p.X + m.M21 * p.Y + m.M31;
            float y = m.M12 * p.X + m.M22 * p.Y + m.M32;
            var v3 = new Vector2(x , y );
            return v3;
        }

        void Init()
        {
            this.ListView.ItemsSource = this.CreatePages(typeof(MainPage)).OrderBy(c => c.Name);

            this.ListView.ItemClick += (s, e) =>
            {
                if (e.ClickedItem is Type item)
                {
                    this.View.Title = item.Name;
                    this.Frame.Navigate(item);
                }
            };

        }

        private IEnumerable<Type> CreatePages(Type assemblyType)
        {
            Assembly assembly = assemblyType.GetTypeInfo().Assembly;
            IEnumerable<TypeInfo> typeInfos = assembly.DefinedTypes;

            foreach (TypeInfo typeInfo in typeInfos)
            {
                string name = typeInfo.Name; // $"{Main}Page"
                string fullName = typeInfo.FullName; // $"Luo_Painter.TestApp.{Main}Page"

                if (name == "MainPage") continue;
                if (name.EndsWith("Page"))
                {
                    Type type = typeInfo.AsType();
                    yield return type;
                    // yield return Activator.CreateInstance(type);
                }
            }
        }

    }

}